package com.bishe.demo.modules.common.service.impl;



import com.bishe.demo.modules.common.dao.ExceptionLogDao;
import com.bishe.demo.modules.common.entity.ExceptionLog;
import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.common.service.ExceptionLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Optional;


@Service
public class ExceptionLogServiceImpl implements ExceptionLogService {

    @Autowired
    private ExceptionLogDao exceptionLogDao;

    @Override
    @Transactional
    public ResultEntity<ExceptionLog> insertExceptionLog(ExceptionLog exceptionLog) {
        List<ExceptionLog> exceptionLogs = Optional
                .ofNullable(exceptionLogDao.getExceptionLogsByParameters(exceptionLog))
                .orElse(Collections.emptyList());
        if (exceptionLogs.isEmpty()) {
            exceptionLogDao.insertExceptionLog(exceptionLog);
        }

        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.code,
                "Insert success", exceptionLog);
    }
}
